package com.caojing.haolemai.ui.outlets.outletsSharefragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.caojing.haolemai.R;
import com.caojing.haolemai.base.BaseFragment;
import com.caojing.haolemai.bean.OutletsSharefragmentBean;
import com.caojing.haolemai.config.UrlConfig;
import com.caojing.haolemai.ui.outlets.outletsdetail.OutletsDetailActivity;
import com.handmark.pulltorefresh.library.LoadingLayoutProxy;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class OutletsShareFragment extends BaseFragment implements OutletsShareFragmentContract.View {

    @Bind(R.id.outletsssharefragment_pull_listview)
    PullToRefreshListView outletsssharefragmentPullListview;

    private OutletsShareFragmentContract.Presenter presenter;
    private OutletsShareFragmentAdapter adapter;
    private int PAGES = 1;
    private LoadingLayoutProxy llProxy;//设置刷新时的文本等的对象

    private List<OutletsSharefragmentBean> totalList;
    private List<OutletsSharefragmentBean.ResultsEntity.ResultsEntity1> resultsEntity1List;


    @Override
    protected void initData() {
        presenter = new OutletsShareFragmentPresenter(this);
        presenter.getVertical("PAGES", initParams());

        outletsssharefragmentPullListview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                outletsssharefragmentPullListview.onRefreshComplete();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                PAGES++;
                presenter.getVertical(""+PAGES++,initParams());
                outletsssharefragmentPullListview.onRefreshComplete();
            }
        });
        outletsssharefragmentPullListview.setScrollingWhileRefreshingEnabled(true);
        llProxy = (LoadingLayoutProxy) outletsssharefragmentPullListview.getLoadingLayoutProxy(true, false);

        llProxy.setRefreshingLabel("下拉刷新");//正在刷新时的文本
        llProxy.setReleaseLabel("松手更新");// 设置控件拉到一定的距离时显示的文本
        //设置加载的图片
//        llProxy.setLoadingDrawable(getResources().getDrawable(R.mipmap.running_people_1));

        //指定刷新的模式 BOTH具有上下拉刷新  PULL_FROM_START 下拉   PULL_FROM_END上拉
        outletsssharefragmentPullListview.setMode(PullToRefreshBase.Mode.BOTH);




        outletsssharefragmentPullListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Intent intent = new Intent(getContext(), OutletsDetailActivity.class);
                intent.putExtra("id",resultsEntity1List.get(i-1).getId());
                Log.i("tag","从outletssharefragment向detailactivity传递"+resultsEntity1List.get(i-1).getId());
                startActivity(intent);
            }
        });

    }

    @Override
    protected void initView(View view) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_outlets_share;
    }

    //这里是为了复用fragment写的一个方法：
    public static OutletsShareFragment newInstance(String argument) {
        Bundle bundle = new Bundle();
        bundle.putString("cat_id", argument);
        OutletsShareFragment outletsShareFragment = new OutletsShareFragment();
        outletsShareFragment.setArguments(bundle);
        return outletsShareFragment;
    }

    public Map<String, String> initParams() {
//       这里需要将cad_id的值传递给每个fragment，作为地址的参数。
        Bundle bundle = getArguments();
        String cat_id = bundle.getString("cat_id");
//        Log.i("TTAA", "需要的tag最新的" + cat_id);

        Map<String, String> params = new HashMap<>();
        params.put(UrlConfig.OutletsKey.SESSION_ID, UrlConfig.OutletsValue.SESSION_ID);
        params.put(UrlConfig.OutletsKey.APP_ID, UrlConfig.OutletsValue.APP_ID);
        params.put(UrlConfig.OutletsKey.SITE_MASK, UrlConfig.OutletsValue.SITE_MASK);
//        params.put(UrlConfig.OutletsKey.CAT_ID, "");
        params.put(UrlConfig.OutletsKey.CAT_ID, cat_id);
        params.put(UrlConfig.OutletsKey.OS, UrlConfig.OutletsValue.OS);
        params.put(UrlConfig.OutletsKey.VERSION, UrlConfig.OutletsValue.VERSION);
        return params;
    }

    @Override
    public void onGetVerticalSucess(OutletsSharefragmentBean outletsSharefragmentBean) {
//        totalList.add(outletsSharefragmentBean);


        if (outletsSharefragmentBean != null) {
            resultsEntity1List = outletsSharefragmentBean.getResults().getResults();

            adapter = new OutletsShareFragmentAdapter(resultsEntity1List, getContext());
            adapter.notifyDataSetChanged();
            outletsssharefragmentPullListview.setAdapter(adapter);
        }
//        Log.i("tag", "网络数据成功");


    }

    @Override
    public void onGetVerticalFail(String erro) {
        Log.i("tag", "网络数据下载失败");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
